package com.bbx.taxi.client.DB;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bbx.api.gclient.GMsg;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

/** 
 * @Path com.bbx.taxi.client.DB.TagMessageDB.java
 * @Author 严旭文
 * @Date 2016-4-14 上午10:24:11
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class TagMessageDB  implements DBcolumns{
	private DBHelper helper;
	public TagMessageDB(Context context) {
		helper = DBHelper.getInstance(context, null);
	}
	/**
	 *  打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() { 
		SQLiteDatabase SQLdb = helper.getReadableDatabase();
		return SQLdb;
	}

	/**
	 *  打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() { 
		SQLiteDatabase SQLdb = helper.getWritableDatabase();
		return SQLdb;
	}
	/**
	 *查询全部
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_TAGMSG, null, null, null, null,
				null, TAG_ID + " desc");
		return cursor;

	}
	/**
	 * 删除整个表
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_TAGMSG, null, null);
		SQLdb.close();
	}
	/**
	 * 插入一条数据 
	 */
	public void onWrite(String name) {

			SQLiteDatabase SQLdb=onOpenWrite();
			ContentValues values = new ContentValues();
			values.put(TAG_NAME,name);
			SQLdb.insert(TABLE_TAGMSG, TAG_ID, values);
			SQLdb.close();

	}
	public void onWrite(final List<String> list){
		onDelete();
		new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... arg0) {
				for(int i=0;i<list.size();i++){
					onWrite(list.get(i));
				}
				return null;
			}}.execute();
	}
	public List<String> onRead(Context context){
		List<String> list=new ArrayList<String>();
		Cursor c = onQuery();
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
			    	}
				list.add(map.get(DBcolumns.TAG_NAME));
				} while (c.moveToNext());
			}
		List<String> list_sort=new ArrayList<String>();
		for(int i=list.size()-1;i>=0;i--){
			list_sort.add(list.get(i));
		}
		return list_sort;
		
	}
	/**
	 * 关闭数据库
	 */
	public void onColse(){
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.close();
	}
}
